layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    let path = ctx + "/sysdict";

    table.render({
        elem: '#currentTableId',
        url: path + "/list",
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {width: 80, title: '序号', templet: function(d){
                return (d.LAY_INDEX);
            }},
            {field: 'type',  title: '类型'},
            {field: 'remarks', title: '备注'},
            {field: 'name',  title: '名称'},
            {field: 'value',  title: '值'},
            {field: 'createTime', title: '创建时间', sort: true, templet: function(d){
                return formatDate(d.createDate);
            }},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code == 200? 0:1, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            }
        },
        limits: [2, 5, 10, 15, 20],
        limit: 5,
        page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {

        // for (let key in data.field){
        //     if (!data.field[key]){
        //         // 字段没有值，则删除该字段
        //         delete data.field[key];
        //     }
        // }
        //
        // console.log(data.field)


        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: data.field

        }, false);

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            var index = layer.open({
                title: '添加类型',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: false,
                area: ['80%', '100%'],
                content: path + '/add',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            // 批量删除
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            if (data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 5});
                return;
            }
            // 获取 id
            var ids = [];
            for (let i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            // 确认删除吗？
            layer.confirm('真的删除选中的数据吗？', function (index) {
                fetch(ctx + '/sysuser/test', {
                    method: 'post',
                    headers: {
                        'Content-Type': 'application/json'

                    },
                    body: JSON.stringify(ids),
                }).then(function (response) {
                    return response.json();
                }).then(function (data) {
                    if (data.code == 200){
                        layer.msg('删除成功', {icon: 1, time: 1000});
                        table.reload('currentTableId');
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                    }
                }).catch(function (e) {
                    layer.msg(e.msg, {icon: 2, time: 1000});
                });
            });

        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {

            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: false,
                area: ['80%', '90%'],
                content: ctx + '/sysuser/edit/' + data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除【' + data.name + '】么', function (index) {
                // 发送请求
                // 成功 obj.del();  layer.close(index);
            });
        } else if (obj.event === 'reset'){
            layer.confirm('真的重置【' + data.name + '】的密码吗？', function (index) {
                // 发送请求
                // 成功 obj.del();  layer.close(index);
            });
        }
    });


    function formatDate(timestamp){
        let date = new Date(timestamp);
        let year = date.getFullYear();
        let month = date.getMonth()+1;
        month =  month < 10? '0' + month : month;
        let day = date.getDate();
        let hour = date.getHours();
        let min = date.getMinutes();
        let sec = date.getSeconds();
        return year+"-"+month+"-"+day+" "+hour+":"+min+":"+sec;
    }

});